﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Vitruvian.Serialization;
using Vitruvian.Serialization.Formatters;
using Vitruvian.Windows.Serialization.Formatters;
using Vitruvian.Logging;
using Vitruvian.Testing;

namespace SpotItComponents
{
    [ContainsTests("Components.General")]
    [OptimisticSerialization]
    public class Component : IComparable
    {
        #region Public Properties
        public Guid Id { get; set; }
        #endregion

        #region Constructor
        public Component()
        {
            Id = Guid.NewGuid();
        }
        #endregion

        #region Method for IComparable Interface
        public int CompareTo(object obj)
        {
            int result = 1;
            if (obj != null)
            {
                if (obj.GetType() == this.GetType())
                {
                    result = this.Id.CompareTo(((Component) obj).Id);
                }
                else
                    result = this.GetType().ToString().CompareTo(obj.GetType().ToString());
            }
            return result;
        }

        public override bool Equals(object obj)
        {
            return (CompareTo(obj) == 0);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion

        #region Unit Test
#if (DEBUG)
        [Test(2)]
        private void Test_Compare()
        {
            Component c1 = new Component();
            Component c2 = new Component();

            Assert.IsEqual(0, c1.CompareTo(c1));
            Assert.IsEqual(c1, c1);
            Assert.IsNotEqual(0, c1.CompareTo(c2));
            Assert.IsNotEqual(0, c2.CompareTo(c1));
            Assert.IsNotEqual(c1, c2);
            Assert.IsNotEqual(c2, c1);

            c2.Id = c1.Id;
            Assert.IsEqual(0, c1.CompareTo(c2));
            Assert.IsEqual(0, c2.CompareTo(c1));
            Assert.IsEqual(c1, c2);
            Assert.IsEqual(c2, c1);

            Symbol s1 = new Symbol();
            Assert.IsEqual(-1, c1.CompareTo(s1));
            Assert.IsEqual(1, s1.CompareTo(c1));

            Assert.IsEqual(1, c1.CompareTo(null));
            Assert.IsEqual(1, s1.CompareTo(null));

        }
#endif
        #endregion

    }
}
